package vicarvision;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Arrays;

/**
 *
 * @author tom
 */
public class Vicarvision {

    private static int myChar;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {



        try {
            /* create address */
            InetAddress serveraddress = InetAddress.getByName("127.0.0.1");
            try (Socket server = new Socket(serveraddress, 9090)) {


                String start_detaillog_xml = "C:\\Users\\User\\Documents\\NetBeansProjects\\Vicarvision\\src\\vicarvision\\xml\\start_detaillog.xml";
                String type = "FaceReaderAPI.Messages.ActionMessage";
                byte[] start_detaillog = apiencrypt.encrypt(start_detaillog_xml, type);

                String start_analysing_xml = "C:\\Users\\User\\Documents\\NetBeansProjects\\Vicarvision\\src\\vicarvision\\xml\\start_analysation.xml";
                byte[] start_analysing = apiencrypt.encrypt(start_analysing_xml, type);
                /* write some data */

                OutputStream socketOutputStream = server.getOutputStream();
                socketOutputStream.write(start_detaillog);
                socketOutputStream.write(start_analysing);


                InputStream stream = server.getInputStream();
                int teller = 1;
                while ((myChar = stream.read()) != -1) {

                    byte[] data = new byte[2048];
                    int count = stream.read(data);
                    if (teller == 1) {
                        data = Arrays.copyOfRange(data, 45, 2048);
                    } else if (teller == 2) {
                        data = Arrays.copyOfRange(data, 40, 2048);
                    } else {
                        data = Arrays.copyOfRange(data, 40, 2048);
                    }
                    String output = new String(data);
//                    output = output.replace("\"", "\\\"");
                    System.out.println(output);
                    System.out.println("----");
                    teller++;


//                    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
//                    DocumentBuilder b = f.newDocumentBuilder();
//                    Document doc = b.parse(new ByteArrayInputStream(output.getBytes("UTF-8")));
//                    NodeList books = doc.getElementsByTagName("ClassificationValues");
//                    for (int i = 0; i < books.getLength(); i++) {
//                        Element book = (Element) books.item(i);
//                        Node title = book.getElementsByTagName("Label").item(0);
//                        System.out.println(title.getTextContent());
//                    }

                }
                server.close();
            }
        } catch (Exception e) {
            System.out.println(e);
        }


    }
}